import matplotlib.pyplot as plt
plt.ion()
class DynamicUpdate():
    #Suppose we know the x range
    min_x = 0
    max_x = 10

    def on_launch(self):
        #Set up plot
        self.figure, self.ax = plt.subplots()
        self.lines, = self.ax.plot([],[], 'o')
        #Autoscale on unknown axis and known lims on the other
        self.ax.set_autoscaley_on(True)
        self.ax.set_xlim(self.min_x, self.max_x)
        #Other stuff
        self.ax.grid()
        #...

    def on_launch2(self):
        import numpy as np
        x1 = np.linspace(0.0, 5.0)
        x2 = np.linspace(0.0, 2.0)

        y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
        y2 = np.cos(2 * np.pi * x2)

        plt.subplot(2, 1, 1)
        plt.plot(x1, y1, 'ko-')
        plt.title('A tale of 2 subplots')
        plt.ylabel('Damped oscillation')

        plt.subplot(2, 1, 2)
        plt.plot(x2, y2, 'r.-')
        plt.xlabel('time (s)')
        plt.ylabel('Undamped')

        plt.show()


    def on_running(self, xdata, ydata):
        #Update data (with the new _and_ the old points)
        self.lines.set_xdata(xdata)
        self.lines.set_ydata(ydata)
        #Need both of these in order to rescale
        self.ax.relim()
        self.ax.autoscale_view()
        #We need to draw *and* flush
        self.figure.canvas.draw()
        self.figure.canvas.flush_events()

    #Example
    def __init__(self,xdata,ydata):
        import numpy as np
        import time
        self.on_launch()
        #self.on_launch2()
        xdata = []
        #ydata = []
        for x in np.arange(0,100,0.5):
            xdata.append(x)
            #ydata.append(np.exp(-x**2)+10*np.exp(-(x-7)**2))
            #print(ydata)
            self.on_running(xdata, ydata)
            time.sleep(1)
        return xdata, ydata

#d = DynamicUpdate()
#d()